Defining Variables
In programming, a variable is a storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value.
The variable name is used to reference this stored value in a computer program.
Imagine you have a backpack where you can store books with each book labeled clearly on the spine. When you need a particular book, you simply look for its label and pull it out.
In Python, a variable is like that label on a book’s spine.
Let's say you have an important number like days until vacation, which is 67. You create a variable called "days_until_vacation" and store the number 67 it.
Like this:
days_until_vacation=67
Now, days_until_end acts like that label on the spine of a book. Whenever you refer to days_until_vacation in your Python code, Python knows you are talking about the number 67. This makes it easy to use and update the information in your code. If the number of days changes, you only need to update the number in one place, and every reference to days_until_vacation will use the new value.
Unlike many other languages Python is dynamically typed meaning you don't need to explicitly declare a variables data type. Here are a few examples:
age = 25
name= "Mollie"
cost=4.5
You can use the assigned statement in many ways. For example:
age = "25"
name= "Mollie"
cost=4.5
print(name + " is " + str(age) + " years old.")
The output for this would be:
Mollie is 25 years old.
Try creating a variable, and tying it with another. Then printing it out.
Number types
Integer
Computers process data values that represent different types of information. Each value in a Python program has a specific type, which determines how it's represented in the system and what operations can be applied to it. The basic types provided by Python are called primitive data types, and include types like numbers, text strings, files, containers, among others. Additionally, Python allows for the creation of custom data types but that is out of the scope of this class.Python recognizes many different numeric data types. An Integer or int is a whole number. For example, the number of people in a room must be an integer. This is because you can not have half a person in a room.
Float
When a value includes a fraction, such as 1.5, Python uses floating-point numbers, referred to as float
cost = 5 # Initially an integer
cost = 5.5 # Later becomes a float
cost = "5" # This is possible but not advisable
cost = 5.0 # Indicates that taxRate may include a fractional part
Variable Names
When creating a variable it is important that you give them a name that explains what it is. whenever you name something in Python, you must follow a few simple rules:
- names must start with a letter or _ and the rest of the characters in the name must be a letter, number, or underscore.
- You CANNOT use any other symbol. Spaces are not allowed inside a name either.
- You CAN use uppercase and lowercase. Python is case-sensitive.
- You cannot used Reserved words such as class if as a name. These are reserved exclusively for Python actions. You can use them within a name like "if_only_i_could_fly"
- Its is best to use a descriptive name like "apples_in_bag" instead of using a name like "aib"
- Most programmers use variables that start with a lowercase letter and uppercase letters to indicate constants.